﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeTableManagement.Entities;
using TimeTableManagement;

namespace TimeTableManagement.Controllers
{ 
    public class TimatableController : Controller
    {
        private QLSVContext db = new QLSVContext();

        //
        // GET: /Timatable/

        public ViewResult Index()
        {
            var tb_timetable = db.Tb_Timetable.Include(t => t.Tb_Course).Include(t => t.Tb_Lecturer).Include(t => t.Tb_Room).Include(t => t.Tb_Term);
            return View(tb_timetable.ToList());
        }

        //
        // GET: /Timatable/Details/5

        public ViewResult Details(int id)
        {
            Tb_Timetable tb_timetable = db.Tb_Timetable.Find(id);
            return View(tb_timetable);
        }

        //
        // GET: /Timatable/Create

        public ActionResult Create()
        {
            ViewBag.id_course = new SelectList(db.Tb_Course, "id", "code");
            ViewBag.id_lecturer = new SelectList(db.Tb_Lecturer, "id", "code");
            ViewBag.id_room = new SelectList(db.Tb_Room, "id", "name");
            ViewBag.id_term = new SelectList(db.Tb_Term, "id", "name");
            return View();
        } 

        //
        // POST: /Timatable/Create

        [HttpPost]
        public ActionResult Create(Tb_Timetable tb_timetable)
        {
            if (ModelState.IsValid)
            {
                db.Tb_Timetable.Add(tb_timetable);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.id_course = new SelectList(db.Tb_Course, "id", "code", tb_timetable.id_course);
            ViewBag.id_lecturer = new SelectList(db.Tb_Lecturer, "id", "code", tb_timetable.id_lecturer);
            ViewBag.id_room = new SelectList(db.Tb_Room, "id", "name", tb_timetable.id_room);
            ViewBag.id_term = new SelectList(db.Tb_Term, "id", "name", tb_timetable.id_term);
            return View(tb_timetable);
        }
        
        //
        // GET: /Timatable/Edit/5
 
        public ActionResult Edit(int id)
        {
            Tb_Timetable tb_timetable = db.Tb_Timetable.Find(id);
            ViewBag.id_course = new SelectList(db.Tb_Course, "id", "code", tb_timetable.id_course);
            ViewBag.id_lecturer = new SelectList(db.Tb_Lecturer, "id", "code", tb_timetable.id_lecturer);
            ViewBag.id_room = new SelectList(db.Tb_Room, "id", "name", tb_timetable.id_room);
            ViewBag.id_term = new SelectList(db.Tb_Term, "id", "name", tb_timetable.id_term);
            return View(tb_timetable);
        }

        //
        // POST: /Timatable/Edit/5

        [HttpPost]
        public ActionResult Edit(Tb_Timetable tb_timetable)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tb_timetable).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.id_course = new SelectList(db.Tb_Course, "id", "code", tb_timetable.id_course);
            ViewBag.id_lecturer = new SelectList(db.Tb_Lecturer, "id", "code", tb_timetable.id_lecturer);
            ViewBag.id_room = new SelectList(db.Tb_Room, "id", "name", tb_timetable.id_room);
            ViewBag.id_term = new SelectList(db.Tb_Term, "id", "name", tb_timetable.id_term);
            return View(tb_timetable);
        }

        //
        // GET: /Timatable/Delete/5
 
        public ActionResult Delete(int id)
        {
            Tb_Timetable tb_timetable = db.Tb_Timetable.Find(id);
            return View(tb_timetable);
        }

        //
        // POST: /Timatable/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Tb_Timetable tb_timetable = db.Tb_Timetable.Find(id);
            db.Tb_Timetable.Remove(tb_timetable);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}